// @/stores/alarmCenter.js
import { defineStore } from 'pinia';

export const useAlarmCenterStore = defineStore('alarmCenter', {
  state: () => ({
    currentAlarm: null, // 单个告警对象 { id, message, type, timestamp }，初始为 null
    nextId: 1, // 用于生成唯一 ID
  }),

  actions: {
    /**
     * 添加或更新当前告警（覆盖旧的）
     */
    addAlarm(message, type = 'unknown') {
      const id = this.nextId++;

      this.currentAlarm = {
        id,
        ...message,
        type,
        timestamp: new Date(),
      };

      return id;
    },

    /**
     * 手动关闭当前告警
     */
    removeAlarm() {
      this.currentAlarm = null;
    },

    /**
     * 清空所有（兼容调用）
     */
    clearAll() {
      this.currentAlarm = null;
      this.nextId = 1;
    },
  },

  getters: {
    hasAlarm: (state) => !!state.currentAlarm,
  },
});
